package com.night.attendance.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Dark-Morning
 * 2022/10/20 15:06
 * 配置消息队列用于签到流量削峰
 */
@Configuration
public class RabbitMQConfiguration {
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("smart-school-attendance");
    }

    @Bean
    public Queue attendanceQueue() {
        Queue queue = new Queue("attendance-queue");
        //设置私信交换机
        queue.addArgument("x-dead-letter-exchange", "attendance-death-exchange");
        //x-dead-letter-routing-key  这里声明当前队列的死信路由key
        queue.addArgument("x-dead-letter-routing-key", "death");
        return queue;
    }
    @Bean
    public Binding deathDirectBinding() {
        return BindingBuilder.bind(deathQueue()).to(directDeathExchange()).with("death");
    }
    @Bean
    public DirectExchange directDeathExchange() {
        return new DirectExchange("attendance-death-exchange");
    }
    @Bean
    public Queue deathQueue() {
        return new Queue("attendance-death-queue");
    }

    @Bean
    public Binding attendanceBinding() {
        return BindingBuilder.bind(attendanceQueue()).to(directExchange()).with("attendance");
    }
}
